/*
 * @lc app=leetcode.cn id=513 lang=javascript
 *
 * [513] 找树左下角的值
 */

// @lc code=start
/**
 * Definition for a binary tree node.
 * function TreeNode(val, left, right) {
 *     this.val = (val===undefined ? 0 : val)
 *     this.left = (left===undefined ? null : left)
 *     this.right = (right===undefined ? null : right)
 * }
 */
/**
 * @param {TreeNode} root
 * @return {number}
 */


var findBottomLeftValue = function(root) {
  //  当前最大深度
  let maxK = -1;
  // 最大值
  let val = 0;

  function dfs(root, curK){
    if(!root) return;
    if(curK > maxK) {
      val = root.val;
      maxK = curK;
    }
    dfs(root.left, curK + 1);
    dfs(root.right, curK + 1);
  }

  dfs(root, 0);
  return val;
};
// @lc code=end

